package com.ruicar.afs.cloud.afscase.loanapprove.task.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.task.condition.LoanApproveTaskCondition;
import com.ruicar.afs.cloud.afscase.loanapprove.task.mapper.LoanApproveTaskMapper;
import com.ruicar.afs.cloud.afscase.loanapprove.task.service.LoanApproveTaskService;
import com.ruicar.afs.cloud.afscase.loanapprove.task.vo.LoanApproveTaskVO;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Description：放款审核任务处理接口实现类</p>
 *
 * @author xueyu.ma
 * @Version 1.0
 * @CreateDate 2020-5-25
 */
@Service
@AllArgsConstructor
@Slf4j
public class LoanApproveTaskServiceImpl implements LoanApproveTaskService {

    private final LoanApproveTaskMapper loanApproveTaskMapper;

    private final CaseContractInfoService caseContractInfoService;

    @Override
    public IPage<LoanApproveTaskVO> getLoanApproveTaskVOList(Page page, LoanApproveTaskCondition loanApproveTaskCondition) {
        return this.loanApproveTaskMapper.getLoanApproveTaskVOList(page, loanApproveTaskCondition);
    }

    @Override
    public IPage<LoanApproveTaskVO> getloanDealWithTaskList(Page page, LoanApproveTaskCondition loanApproveTaskCondition) {
        return this.loanApproveTaskMapper.getloanDealWithTaskList(page, loanApproveTaskCondition);
    }


    @Override
    public IPage<LoanApproveTaskVO> queryCommonPoolTaskList(Page page, LoanApproveTaskCondition loanApproveTaskCondition) {
        return this.loanApproveTaskMapper.queryLoanCommonPoolTaskList(page, loanApproveTaskCondition);
    }
    @Override
    public IPage<LoanApproveTaskVO> queryWaitReviewPoolTaskList(Page page, LoanApproveTaskCondition loanApproveTaskCondition) {
        return this.loanApproveTaskMapper.queryWaitReviewPoolTaskList(page, loanApproveTaskCondition);
    }
    @Override
    public IPage<LoanApproveTaskVO> queryAllPoolTaskList(Page page, LoanApproveTaskCondition loanApproveTaskCondition) {
        return this.loanApproveTaskMapper.queryLoanAllPoolTaskList(page, loanApproveTaskCondition);
    }

    @Override
    public Integer modifyLock(List contractNoList, String status, String lockRemark) {
        return this.loanApproveTaskMapper.modifyLock(contractNoList, status, lockRemark);
    }

    /**
     * 添加标签
     *
     * @param contractNoList 申请编号列表
     * @param label
     */
    @Override
    public Integer modifyLabel(List contractNoList, String label) {
        return this.loanApproveTaskMapper.modifyLabel(contractNoList,label);
    }

    /**
     * 置顶
     *
     * @param contractNoList 申请编号列表
     * @param status         置顶状态
     */
    @Override
    public Integer modifyTop(List contractNoList, String status) {
        return this.loanApproveTaskMapper.modifyTop(contractNoList,status);
    }

    /**
     * 标签删除
     *
     * @param contractNoList 申请编号列表
     */
    @Override
    public void delLabelList(List contractNoList) {
        List<CaseContractInfo> caseContractInfoList = caseContractInfoService.list(Wrappers.<CaseContractInfo>query().lambda()
                .in(CaseContractInfo::getContractNo,contractNoList));
        if(CollectionUtils.isNotEmpty(caseContractInfoList)){
            caseContractInfoList.forEach(caseContractInfo -> {
                if(!caseContractInfo.getIsLock().equals(WhetherEnum.YES.getCode())){
                    caseContractInfo.setRemarks("");
                    caseContractInfoService.updateById(caseContractInfo);
                }
            });
        }
    }
}
